﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Input;

namespace PonGun
{
    public class Block : Base2DObject
    {

        public Block(Game1 game)
            : base(game)
        {
            RegisteredCollider = true;
        }

        public override void Collided(CollisionData collisionData)
        {
            base.Collided(collisionData);

            Base2DObject collidedWith = null;
            if (collisionData.Objects[0] == this)
                collidedWith = (Base2DObject)collisionData.Objects[1];
            else
                collidedWith = (Base2DObject)collisionData.Objects[0];

        }

        public override bool Erroded(Base2DObject obj, bool errodBlock = false)
        {

            bool eroded = false;

            #region
            // remove pixels from the texture...

            int xCol = (int)Math.Abs((Position.X - halfWidth) - (obj.Position.X - obj.halfWidth));
            int yRow = (int)Math.Abs((Position.Y - halfHeight) - (obj.Position.Y - obj.halfHeight));

            if (yRow - obj.halfHeight >= 0)
                yRow -= obj.halfHeight;

            if (xCol - obj.halfWidth >= 0)
                xCol -= obj.halfWidth;

            Vector2 colNormal = new Vector2(obj.Velocity.X > 0 ? -1 : 1, 0);
            int tookOut = 0;

            if (colNormal == Game1.leftNormal || colNormal == Game1.rightNormal)
            {
                for (int y = 0; y < obj.Height * 2; y++)
                {
                    if (colNormal == Game1.rightNormal)
                    {
                        for (int x = xCol, xcol = 0; x < Texture2D.Width && xcol < obj.Width * 2; x++, xcol++)
                        {
                            if (TextureMap[x + yRow * Texture2D.Width].A != 0)
                            {
                                if (errodBlock)
                                {
                                    TextureMap[x + yRow * Texture2D.Width].A = 0;

                                    for (int w = 1; w < obj.Height * 2; w++)
                                    {
                                        if (yRow - w > 0 && Game1.rnd.NextDouble() > .5)
                                            TextureMap[x + (yRow - w) * Texture2D.Width].A = 0;

                                        if (w + yRow < Texture2D.Height - 1 && Game1.rnd.NextDouble() > .5)
                                            TextureMap[x + (yRow + w) * Texture2D.Width].A = 0;
                                    }
                                }


                                tookOut++;

                                eroded = true;

                                if (tookOut > obj.Width)
                                    break;

                            }
                        }
                    }

                    if (colNormal == Game1.leftNormal)
                    {
                        for (int x = xCol, xcol = 0; x < Texture2D.Width && xcol < obj.Width * 2; x++, xcol++)
                        {
                            if (TextureMap[x + yRow * Texture2D.Width].A != 0)
                            {
                                if (errodBlock)
                                {
                                    TextureMap[x + yRow * Texture2D.Width].A = 0;

                                    for (int w = 1; w < obj.Height * 2; w++)
                                    {
                                        if (yRow - w > 0 && Game1.rnd.NextDouble() > .5)
                                            TextureMap[x + (yRow - w) * Texture2D.Width].A = 0;

                                        if (w + yRow < Texture2D.Height - 1 && Game1.rnd.NextDouble() > .5)
                                            TextureMap[x + (w + yRow) * Texture2D.Width].A = 0;
                                    }

                                }

                                tookOut++;

                                eroded = true;

                                if (tookOut > obj.Width)
                                    break;

                            }
                        }
                    }

                    yRow++;
                    if (yRow >= Texture2D.Height)
                        yRow = Texture2D.Height - 1;
                }
            }


            if (eroded && errodBlock)
                Texture2D.SetData<Color>(TextureMap);

            #endregion

            return eroded;
        }

        
    }
}
